/*
 * @Author: your name
 * @Date: 2024-04-24 16:36:29
 * @LastEditTime: 2024-04-24 17:09:46
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: \.leetcode\113.路径总和-ii.cpp
 */
/*
 * @lc app=leetcode.cn id=113 lang=cpp
 *
 * [113] 路径总和 II
 */

// @lc code=start
/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
public:
    vector<int> tmp_vec;
    vector<vector<int>> res;
    void traversal(TreeNode* root, int count){
        if(!root->left && !root->right && count == 0){
            res.push_back(tmp_vec);
            return;
        }
        if(!root->left && !root->right){
            return;
        }
        if(root->left){
            tmp_vec.push_back(root->left->val);
            traversal(root->left, count - root->left->val);
            tmp_vec.pop_back();
        }
        if(root->right){
            tmp_vec.push_back(root->right->val);
            traversal(root->right, count - root->right->val);
            tmp_vec.pop_back();
        }
    }

    vector<vector<int>> pathSum(TreeNode* root, int targetSum) {
        if(!root)return {};
        tmp_vec.push_back(root->val);
        traversal(root, targetSum - root->val);
        return res;
    }
};
// @lc code=end

// count需要回溯，tmp_vec数组也需要回溯